package com.mxpio.erp.sales.service.impl;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.sales.entity.*;
import com.mxpio.erp.common.sales.service.SalesOrderLineService;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SalesOrderLineServiceImpl extends BaseServiceImpl<SalesOrderLine> implements SalesOrderLineService {
    @Transactional(readOnly = false)
    @Override
    public List<SalesOrderLine> list(Criteria c){
        /*Map<String,SalesOrder> salesOrderMap = new HashMap<>();
        List<SalesOrderLine> salesOrderLines = this.list(SalesOrderLine.class, c);
        for(SalesOrderLine salesOrderLine:salesOrderLines){
            //gettingQuantity(salesOrderLine);
            SalesOrder salesOrder = salesOrderMap.get(salesOrderLine.getBizNo());
            if(salesOrder==null){
                salesOrder = JpaUtil.linq(SalesOrder.class).idEqual(salesOrderLine.getBizNo()).findOne();
                salesOrderMap.put(salesOrderLine.getBizNo(),salesOrder);
            }
            salesOrderLine.setSalesOrder(salesOrder);
        }*/
        return JpaUtil.linq(SalesOrderLine.class).collect("bizNo", SalesOrder.class , "bizNo").where(c).list();
    }

    @Transactional(readOnly = false)
    @Override
    public List<SalesOrderLine> auditList(Criteria c){
        List<SalesOrderLine> salesOrderLineList = list(c);
        if(CollectionUtils.isNotEmpty(salesOrderLineList)){
            salesOrderLineList.removeIf(line -> !line.getSalesOrder().getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode()));
        }
        return salesOrderLineList;
    }

    @Transactional(readOnly = false)
    @Override
    public Page<SalesOrderLine> listPage(Pageable page, Criteria c){
		return JpaUtil.linq(SalesOrderLine.class)
					.collect("bizNo", SalesOrder.class, "bizNo")
					.exists(SalesOrder.class)
						.equalProperty("bizNo", "bizNo")
						.equal("bpmnStatus", OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())
					.end()
					.where(c)
					.paging(page);
    }

    @Transactional(readOnly = false)
    @Override
    public SalesOrderLine getById(OrderLineKey id){
        SalesOrderLine salesOrderLine = this.getById(SalesOrderLine.class,id);
        if(salesOrderLine!=null){
            SalesOrder salesOrder = JpaUtil.linq(SalesOrder.class).idEqual(salesOrderLine.getBizNo()).findOne();
            salesOrderLine.setSalesOrder(salesOrder);
        }
        return salesOrderLine;
    }

    /*private void gettingQuantity(SalesOrderLine salesOrderLine){
        salesOrderLine.setDeliverQuantity(BigDecimal.ZERO);
        salesOrderLine.setActualMaxQuantity(salesOrderLine.getQuantity());
        salesOrderLine.setPlanQuantity(BigDecimal.ZERO);
        salesOrderLine.setPlanBackQuantity(BigDecimal.ZERO);
        salesOrderLine.setActualBackQuantity(BigDecimal.ZERO);
        //计划发货数
        List<ShippingNotice> noticeList = JpaUtil.linq(ShippingNotice.class).equal("bizOrderNo", salesOrderLine.getBizNo()).list();
        if(CollectionUtils.isEmpty(noticeList)){
            return;
        }
        Set<String> noticeNoSet = noticeList.stream().map(ShippingNotice::getNoticeNo).collect(Collectors.toSet());
        List<ShippingNoticeLine> noticeLineList = JpaUtil.linq(ShippingNoticeLine.class).in("noticeNo",noticeNoSet).equal("itemCode",salesOrderLine.getItemCode()).list();
        BigDecimal planQuantity = noticeLineList.stream().map(ShippingNoticeLine::getQuantity).reduce(BigDecimal.ZERO,BigDecimal::add);
        salesOrderLine.setPlanQuantity(planQuantity);
        salesOrderLine.setActualMaxQuantity(salesOrderLine.getActualMaxQuantity().subtract(planQuantity));
        //实际发货数
        BigDecimal deliverQuantity = getActual(salesOrderLine,OrderEnums.AccessType.OUT.getCode());
        if(deliverQuantity==null){
            return;
        }
        salesOrderLine.setDeliverQuantity(deliverQuantity);
        //计划发货数不包括实际发货数
        salesOrderLine.setPlanQuantity(planQuantity.subtract(deliverQuantity));


        //计划退货数
        List<RejectBill> rejectList = JpaUtil.linq(RejectBill.class).in("bizOrderNo",noticeNoSet).list();
        if(CollectionUtils.isEmpty(rejectList)){
            return;
        }
        Set<String> rejectNoticeNoSet = rejectList.stream().map(RejectBill::getNoticeNo).collect(Collectors.toSet());
        List<RejectBillLine> rejectLineList = JpaUtil.linq(RejectBillLine.class).in("noticeNo",rejectNoticeNoSet).equal("itemCode",salesOrderLine.getItemCode()).list();
        BigDecimal planBackQuantity = rejectLineList.stream().map(RejectBillLine::getQuantity).reduce(BigDecimal.ZERO,BigDecimal::add);
        salesOrderLine.setPlanBackQuantity(planBackQuantity);

        salesOrderLine.setActualMaxQuantity(salesOrderLine.getActualMaxQuantity().add(planBackQuantity));

        //实际退货数
        BigDecimal actualBackQuantity = getActual(salesOrderLine,OrderEnums.AccessType.IN.getCode());
        if(actualBackQuantity==null){
            return;
        }
        salesOrderLine.setActualBackQuantity(actualBackQuantity);
        //计划退货数不包括实际退货数
        salesOrderLine.setPlanBackQuantity(planBackQuantity.subtract(actualBackQuantity));


    }*/

    /*private BigDecimal getActual(SalesOrderLine salesOrderLine,String accessType){
        Set<String> processingStatus = new HashSet<>();
        processingStatus.add(OrderStatusEnums.OrderStatus.PROCESSING.getStatusCode());
        processingStatus.add(OrderStatusEnums.OrderStatus.FINISHED.getStatusCode());
        processingStatus.add(OrderStatusEnums.OrderStatus.CLEARED.getStatusCode());
        List<WarehouseOrder> warehouseOrderList = JpaUtil.linq(WarehouseOrder.class).equal("originBizNo",salesOrderLine.getBizNo()).in("orderStatus", processingStatus).equal("accessType",accessType).list();
        if(CollectionUtils.isEmpty(warehouseOrderList)){
            return null;
        }
        Set<String> warehouseOrderNoSet = warehouseOrderList.stream().map(WarehouseOrder::getBizNo).collect(Collectors.toSet());
        List<WarehouseOrderLine> warehouseOrderLineList = JpaUtil.linq(WarehouseOrderLine.class).in("bizNo",warehouseOrderNoSet).equal("itemCode",salesOrderLine.getItemCode()).list();
        return warehouseOrderLineList.stream().map(WarehouseOrderLine::getQuantity).reduce(BigDecimal.ZERO,BigDecimal::add);
    }*/
}
